package com.jinzecloud.reportapproval.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.jinzecloud.reportapproval.bean.Admin;
import com.jinzecloud.reportapproval.bean.RoleEntity;
import com.jinzecloud.reportapproval.bean.RoleUserEntity;
import com.jinzecloud.reportapproval.form.RoleUserQueryForm;
import com.jinzecloud.reportapproval.form.RoleUserUpdateForm;
import com.jinzecloud.reportapproval.manager.RoleEmployeeManager;
import com.jinzecloud.reportapproval.mapper.AdminMapper;
import com.jinzecloud.reportapproval.mapper.RoleMapper;
import com.jinzecloud.reportapproval.mapper.RoleUserMapper;
import com.jinzecloud.reportapproval.utils.R;
import com.jinzecloud.reportapproval.utils.SmartBeanUtil;
import com.jinzecloud.reportapproval.vo.RoleSelectedVO;
import com.jinzecloud.reportapproval.vo.RoleVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色-员工
 */
@Service
public class RoleUserService {

    @Resource
    private RoleUserMapper roleUserMapper;
    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleEmployeeManager roleEmployeeManager;
    @Resource
    private AdminMapper adminMapper;


    /**
     * 批量插入
     *
     */
    public void batchInsert(List<RoleUserEntity> roleEmployeeList) {
        roleEmployeeManager.saveBatch(roleEmployeeList);
    }

    /**
     * 通过角色id，分页获取用户列表
     *
     */
    public R queryUser(RoleUserQueryForm roleUserQueryForm) {
        //构建查询角色中所有的用户
        QueryWrapper<RoleUserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleUserQueryForm.getRoleId());
        wrapper.select("user_id");
        List<RoleUserEntity> roleUserEntities = roleUserMapper.selectList(wrapper);
        if (roleUserEntities.isEmpty())return R.ok().put("data",new Page<Admin>());
        QueryWrapper<Admin> wrapperUserinfo = new QueryWrapper<>();
//        wrapperUserinfo.like(StringUtils.isNotBlank(roleUserQueryForm.getKeyword()),"blname",roleUserQueryForm.getKeyword());
//        wrapperUserinfo.like(StringUtils.isNotBlank(roleUserQueryForm.getKeyword()),"blaccount",roleUserQueryForm.getKeyword());
//        wrapperUserinfo.like(StringUtils.isNotBlank(roleUserQueryForm.getKeyword()),"blphone",roleUserQueryForm.getKeyword());
        wrapperUserinfo.in("blsysuserid",roleUserEntities.stream().map(RoleUserEntity::getUserId).collect(Collectors.toList()) );
        Page<Admin> page = new Page<>(roleUserQueryForm.getPageNo(), roleUserQueryForm.getPageSize());
        Page<Admin> blsysuserPage = adminMapper.selectPage(page, wrapperUserinfo);
        return R.ok().put("data",blsysuserPage);
    }
    /**
     * 移除用户角色
     *
     */
    @Transactional(rollbackFor = Exception.class)
    public R<String> removeRoleUser(Long userId, Long roleId) {
        if (null == userId || null == roleId) {
            return R.error("参数不能为空");
        }
        roleUserMapper.deleteByUserIdRoleId(userId, roleId);
        return R.ok();
    }

    /**
     * 批量删除角色的成员
     *
     */
    public R batchRemoveRoleUser(RoleUserUpdateForm roleUserUpdateForm) {
        roleUserMapper.deleteByUserId(roleUserUpdateForm.getUserIdList());
//        roleUserMapper.batchDeleteUserRole(roleUserUpdateForm.getRoleId(), roleUserUpdateForm.getUserIdList());
        return R.ok();
    }
    /**
     * 批量添加角色的成员
     *
     */
    public R batchAddRoleUser(RoleUserUpdateForm roleUserUpdateForm) {
        List<Long> roleLists =  roleUserUpdateForm.getRoleId();
        Long userId = roleUserUpdateForm.getUserIdList();
        // 保存新的角色员工
        List<RoleUserEntity> roleUserList = null;
        if (CollectionUtils.isNotEmpty(roleLists)) {
            roleUserList = roleLists.stream()
                    .map(roleId -> new RoleUserEntity(roleId, userId))
                    .collect(Collectors.toList());
        }
        // 保存数据
        roleEmployeeManager.saveRoleUser(roleUserList, userId);
        return R.ok();
    }

    /**
     * 通过员工id获取员工角色
     *
     */
    public List<RoleSelectedVO> getRoleInfoListByUserId(Long userId) {
        List<Long> roleIds = roleUserMapper.selectRoleIdByUserId(userId);
        List<RoleEntity> roleList = roleMapper.selectList(new QueryWrapper<>());
        List<RoleSelectedVO> result = SmartBeanUtil.copyList(roleList, RoleSelectedVO.class);
        result.stream().forEach(item -> item.setSelected(roleIds.contains(item.getRoleId())));
        return result;
    }

    /**
     * 根据员工id 查询角色id集合
     *
     */
    public List<RoleVO> getRoleIdList(Long userId) {
        return roleUserMapper.selectRoleUserId(userId);
    }


    public void saveUserRole(RoleUserEntity roleUserEntity) {
        roleUserMapper.insert(roleUserEntity);
    }

    public List<RoleUserEntity> list(QueryWrapper<RoleUserEntity> roleUserEntityQueryWrapper) {
        return roleUserMapper.selectList(roleUserEntityQueryWrapper);
    }

    public void remove(QueryWrapper<RoleUserEntity> roleUserEntityQueryWrapper) {
        roleUserMapper.delete(roleUserEntityQueryWrapper);
    }
}
